package shayu.product.service.impl;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import shayu.product.dao.AttrGroupDao;
import shayu.product.entity.AttrEntity;
import shayu.product.entity.AttrGroupEntity;
import shayu.product.service.AttrGroupService;
import shayu.product.service.AttrService;
import shayu.product.vo.AttrGroupWithVo;
import shayu.product.vo.SpuItemAttrGroupVo;


@Service("attrGroupService")
public class
AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        //如果传过来的三级分类id为0，就查询所有数据
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page=this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<List<AttrGroupEntity>> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 1. 查出所有分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        // 2. 查出所有属性
        List<List<AttrGroupEntity>> collect = attrGroupEntities.stream().map((item) -> {
            AttrGroupWithVo attrGroupWithVo = new AttrGroupWithVo();
            BeanUtils.copyProperties(item, attrGroupWithVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithVo.getAttrGroupId());
            attrGroupWithVo.setAttrs(attrs);
            return attrGroupEntities;
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 1. 查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        List<SpuItemAttrGroupVo> vos=this.baseMapper.getAttrGroupWithAttrBySpuId(spuId,catalogId);
        return vos;
    }


}

